#include "logger.h"
#include <stdio.h>
#include <stdlib.h>

using namespace std;
int logger::m_level=INFO;

logger::logger(string name)
{
    //ctor

    m_path="log/";
    m_name=name;
    m_Counter=0;
   // printf("con::%s,%s\n",m_path.c_str(),m_name.c_str());
   // string t=m_path+name;
    m_file.open((m_path+name).c_str());
}
char* level_str[]={"ERROR","WARN","INFO","DEBUG"};
 char str[100];
void logger::log(int level,string position,string message )
{

    if(level<=INFO)
    {
        sprintf(str,"%5d[%s in %s]:%s\n",m_Counter,level_str[level-1] ,position.c_str(),message.c_str());

        m_file<<str<<flush;

    }
    if(level<=m_level)
    {
        sprintf(str,"%5d[%s in %s]:%s\n",m_Counter,level_str[level-1] ,position.c_str(),message.c_str());
        printf("%s",str);
    }
    if(level==ERROR)
        exit(1);
    m_Counter++;
}
void  logger::log_few(int level,string message)
{
    if(level<=INFO)
    {
        m_file<<message<<"\n"<<flush;
    }
    if(level<=m_level)
    {
        sprintf(str,"%s\n",message.c_str());
        printf("%s",str);
    }
    if(level==ERROR)
        exit(1);
}
logger::~logger()
{
    //dtor
}
logger& logger::operator<<(string mes)
{
    if(DEBUG==logger::m_level)
    {
          std::cout<<mes;
    }
    return *this;
}
logger& logger::operator<<(int mes)
{
    if(DEBUG==logger::m_level)
    {
          std::cout<<mes;
    }
    return *this;
}
logger& logger::operator<<(float mes)
{
    if(DEBUG==logger::m_level)
    {
          std::cout<<mes;
    }
    return *this;
}
logger& logger::operator<<(long mes)
{
    if(DEBUG==logger::m_level)
    {
          std::cout<<mes;
    }
    return *this;
}
